// Copyright 2022 jeff.li. and/or its affiliates.

#pragma once

#include <map>
#include <memory>
#include <set>

#include <tbir/runtime/algorithm/cedar.h>
#include <tbir/runtime/container/string.h>
#include <tbir/runtime/runtime_port.h>

namespace tbir::runtime {

// Given a list of strings, finds the longest string which is a
// prefix of a query.
    class PrefixMapping {
#if defined(USE_CEDAR_UNORDERED)
        typedef cedar::da<int, -1, -2, false> cedar_t;
#else
        typedef cedar::da<int> cedar_t;
#endif
    public:
        explicit PrefixMapping(const std::map<String, int> &dic);

        // Finds the longest string in dic, which is a prefix of `w`.
        // Returns the UTF8 byte length of matched string.
        // `found` is set if a prefix match exists.
        // If no entry is found, return 0.
        int PrefixSearch(const char *w, size_t w_len, int *val) const;

    private:
        std::unique_ptr<cedar_t> trie_;
    };

}  // namespace tbir::runtime
